package com.hgq.jssjyyxt.controller;

import com.alibaba.fastjson.JSONObject;
import com.hgq.jssjyyxt.entity.ForumComments;
import com.hgq.jssjyyxt.service.ForumCommentsService;
import com.hgq.jssjyyxt.util.Result;

import com.hgq.jssjyyxt.util.ValidToken;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * (ForumComments)表控制层
 *
 * @author makejava
 * @since 2024-04-11 13:53:41
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("forumComments")
public class ForumCommentsController {
    /**
     * 服务对象
     */
    @Resource
    private ForumCommentsService forumCommentsService;
    @Resource
    RedisTemplate<String, Integer> redisTemplate;

    /**
     * 分页查询
     *
     * @param forumComments 筛选条件
     * @param limit         分页对象
     * @param page          分页对象
     * @return 查询结果
     */
    @GetMapping("findAll")
    public Result findAll(ForumComments forumComments, Integer limit, Integer page) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        return forumCommentsService.selectAll(forumComments, limit, page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById")
    public Result getById(Integer id) {
        return Result.success(this.forumCommentsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param forumComments 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody ForumComments forumComments, HttpServletRequest request) {
        //判断是否为空
        if (forumComments == null) {
            return Result.failure("参数不能为空");
        }
        Integer id = ValidToken.getIdByToken(request, redisTemplate);
        forumComments.setUserId(id);
        forumComments.setCreatedAt(new Date());
        if (this.forumCommentsService.save(forumComments)) {
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param forumComments 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody ForumComments forumComments) {
        //判断是否为空
        if (forumComments == null) {
            return Result.failure("参数不能为空");
        }

        if (this.forumCommentsService.updateById(forumComments)) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @param data 主键
     * @return 删除是否成功
     */
    @PostMapping("delete")
    public Result deleteById(@RequestBody JSONObject data) {
        //判断id是否为空
        Integer id = data.getInteger("commentId");
        if (id == null) {
            return Result.failure("id不能为空");
        }

        if (this.forumCommentsService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }

}

